package com.facemox.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.facemox.bean.EnvConfig;
import com.facemox.bean.FaceInfo;
import com.facemox.bean.Module;
import com.facemox.bean.PermissionAuthentication;
import com.facemox.service.EnvConfigService;
import com.facemox.service.FaceInfoService;
import com.facemox.service.ModuleService;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
public class FaceController {
    
    @Autowired
    private FaceInfoService faceInfoService;
    
    @Autowired
    private FaceInfo faceInfo;
    
    @Autowired
    private ModuleService moduleService;
    
    @Autowired
    private EnvConfigService envConfigService;
    /**
     * @module 根据moduleId获取接口列表
     */
    @RequestMapping("get_face_list_by_module_id")
    public List<FaceInfo> getFaceListByModuleId(HttpServletRequest request) {
        String tempMID = request.getParameter("moduleId");
        Integer moduleId = Integer.valueOf(tempMID);
        List<FaceInfo> faceList = faceInfoService.getFaceListByModuleId(moduleId);
        return faceList;
    }
    
    /**
     * @module 根据moduleId获取接口列表带分页
     */
    @RequestMapping("get_face_list_by_module_id_page")
    public List<FaceInfo> getFaceListByModuleIdPage(HttpServletRequest request) {
        String tempMID = request.getParameter("moduleId");
        Integer moduleId = Integer.valueOf(tempMID);
        String pageSize = request.getParameter("pageSize");
        String fromPageStr = request.getParameter("fromPage");
        Integer fromPage = Integer.valueOf(fromPageStr);
        String toPageStr = request.getParameter("toPage");
        Integer toPage = Integer.valueOf(toPageStr);
        
        List<FaceInfo> faceList = faceInfoService.getFaceListByModuleIdPage(moduleId,fromPage,toPage);
        String temp="";
        for (FaceInfo faceInfo : faceList) {
            int length = faceInfo.getFaceBody().length();
            if(30<faceInfo.getFaceBody().length()) {
                temp = faceInfo.getFaceBody().substring(0, 30);
            }else {
                temp = faceInfo.getFaceBody().substring(0, faceInfo.getFaceBody().length());
            }
            faceInfo.setFaceBody(temp);
        }
        return faceList;
    }
    
    /**
     * @module 根据moduleId获取所在module下的接口数量
     */
    @RequestMapping("get_face_list_by_module_id_count")
    public int getFaceListByModuleIdCount(HttpServletRequest request) {
        String tempMID = request.getParameter("moduleId");
        Integer moduleId = Integer.valueOf(tempMID);
        
        int count = faceInfoService.getFaceListByModuleIdCount(moduleId);
        
        return count;
    }
    
    /**
     * @module 根据faceId查询接口信息
     */
    @RequestMapping("find_face_by_id")
    public List<Object> findFace(HttpServletRequest request) {
        String tempFID = request.getParameter("faceId");
        Integer faceId = Integer.valueOf(tempFID);
        
        FaceInfo face = faceInfoService.findFace(faceId);
        
        // JSON格式化
        ObjectMapper mapper = new ObjectMapper();
        try {
            Object obj = mapper.readValue(face.getFaceBody(), Object.class);
            face.setFaceBody(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj));
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        List<Module> moduleList = moduleService.getAllModule();
        List<PermissionAuthentication> authenticationList = moduleService.getAllAuthorization();
        EnvConfig envConfig = envConfigService.getEnvConfig();
        
        String paramBody = envConfig.getParamBody();
        Map<String, Object> map = new HashMap<String,Object>();
        Map<String, String> mapStr = new HashMap<String,String>();
        
        if(paramBody!=null) {
             map = JSON.parseObject(paramBody);
         }
        
        for (String key : map.keySet()) { 
            String value = map.get(key).toString();
            mapStr.put(key,map.get(key).toString());
        }
        
        List<Object> list = new ArrayList<Object>();
        list.add(face);
        list.add(moduleList);
        list.add(authenticationList);
        list.add(mapStr);
        
        return list;
    }
    
    /**
     * @module 根据faceId更新接口信息
     */
    @RequestMapping("update_face")
    public int updateFace(HttpServletRequest request) {
        String tempFID = request.getParameter("display_face_id");
        String faceName = request.getParameter("display_face_name");
        String faceMethod = request.getParameter("display_face_method");
        String faceProtocol = request.getParameter("display_face_protocol");
        String faceBody = request.getParameter("display_face_body");
        String faceHost = request.getParameter("display_face_host");
        String faceUrl = request.getParameter("display_face_url");
        String tempMID = request.getParameter("display_module_id");
        String tempAuthId = request.getParameter("display_auth_name");
        Integer faceId = Integer.valueOf(tempFID);
        Integer moduleId = Integer.valueOf(tempMID);
        Integer authenticationId = Integer.valueOf(tempAuthId);
        faceInfo.setFaceId(faceId);
        faceInfo.setFaceName(faceName);
        faceInfo.setFaceMethod(faceMethod);
        faceInfo.setFaceProtocol(faceProtocol);
        faceInfo.setFaceBody(faceBody);
        faceInfo.setFaceHost(faceHost);
        faceInfo.setFaceUrl(faceUrl);
        faceInfo.setModuleId(moduleId);
        faceInfo.setAuthenticationId(authenticationId);
        int effectLine = faceInfoService.updateFace(faceInfo);
        return effectLine;
    }
    
    /**
     * @module 查询所有的接口
     */
    public List<FaceInfo> getFaceAll(){
        
        List<FaceInfo> faceList = faceInfoService.getFaceAll();
        
        
        
        return new ArrayList<FaceInfo>();
    }
    
}
